<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'bus:pre:yfPreRecord:edit'">
          <Icon icon="simple-line-icons:user-following" /> {{ t('手动分派') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.hospName }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusPreYfPreRecordList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { yfPreRecordDelete, yfPreRecordListPlanData } from '/@/api/bus/pre/yfPreRecord';
  import { officeTreeData } from '/@/api/sys/office';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import console from 'console';
  import { m8ViewRouteTreeData } from '/@/api/bas/route/m8ViewRoute';

  const { t } = useI18n('bus.pre.yfPreRecord');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('预约记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('收运时间'),
        field: 'planDate',
        component: 'RangePicker',
        componentProps: {
          allowEmpty: [true, true],
        },
      },
      {
        label: t('医疗机构'),
        field: 'hospName',
        component: 'Input',
      },
      {
        label: t('线路'),
        field: 'm8ViewHosp.routeCode',
        fieldLabel: 'm8ViewHosp.croute',
        component: 'TreeSelect',
        componentProps: {
          api: m8ViewRouteTreeData,
          params: { isShowCode: false },
          // canSelectParent: false,
          allowClear: true,
        },
      },
      // {
      //   label: t('线路顺序'),
      //   field: 'm8ViewHosp.routeSort',
      //   component: 'Input',
      // },
      // {
      //   label: t('预约人'),
      //   field: 'createBy',
      //   component: 'TreeSelect',
      //   componentProps: {
      //     api: officeTreeData,
      //     params: { isLoadUser: true, userIdPrefix: '' },
      //     canSelectParent: false,
      //     allowClear: true,
      //   },
      // },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'yf_pre_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['planDate', ['planDate_gte', 'planDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('预约时间'),
    //   dataIndex: 'createDate',
    //   key: 'a.create_date',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    {
      title: t('医疗机构'),
      dataIndex: 'hospName',
      key: 'a.hosp_name',
      sorter: true,
      width: 330,
      align: 'left',
      fixed: 'left',
      // slot: 'firstColumn',
    },
    // {
    //   title: t('预约人'),
    //   dataIndex: 'creator.userName',
    //   key: 'a.create_by',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    // },
    {
      title: t('预计收运日期'),
      dataIndex: 'planDate',
      key: 'a.plan_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('路线'),
      dataIndex: 'm8ViewHosp.croute',
      key: 'hosp.croute',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('顺序'),
      dataIndex: 'm8ViewHosp.routeSort',
      key: 'hosp.route_sort',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'driverObj.userName',
      key: 'a.driver',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('押运员'),
      dataIndex: 'driver2Obj.userName',
      key: 'a.driver2',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('车牌号'),
      dataIndex: 'carNo',
      key: 'a.car_no',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'yf_pre_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑预约记录'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bus:pre:yfPreRecord:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除预约记录'),
        popConfirm: {
          title: t('是否确认删除预约记录'),
          confirm: handleDelete.bind(this, { selIds: record.hospCode }),
        },
        auth: 'bus:pre:yfPreRecord:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] = useTable({
    api: yfPreRecordListPlanData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    ellipsis: false,
    rowSelection: { type: 'checkbox' },
  });

  function handleForm(record: Recordable) {
    const selIds = getSelectRowKeys();
    if (!selIds || selIds.length === 0) {
      showMessage('请选择要分派的预约记录', 'error');
      return;
    }
    record.selIds = selIds.join(',');
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await yfPreRecordDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  async function btnBatchDel() {
    const selRows = getSelectRows();
    if (!selRows || selRows.length === 0) {
      showMessage('请选择要取消的预约记录', 'error');
      return;
    }
    const selIds = selRows.map((item) => {
      return item.hospCode;
    });
    const res = await yfPreRecordDelete({ selIds: [...new Set(selIds)].join(',') });
    console.log(res);
    showMessage(res.message);
    handleSuccess();
  }
</script>
